package com.hs.ph.Dao.Impl;

import com.hs.ph.Dao.ShopDao;
import com.hs.ph.Entity.Shop;
import com.hs.ph.Utils.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;


public class ShopDaoImpl implements ShopDao {
    @Override
    public int insert(Shop shop) {
        String sql = "insert into shop values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ";
        Object[] obj = new Object[]{
                shop.getShopName(),
                shop.getShopDescribe(),
                shop.getShopImage(),
                shop.getShopPhone(),
                shop.getShopProvince(),
                shop.getShopCity(),
                shop.getShopDistrict(),
                shop.getShopAddress(),
                shop.getShopLongitude(),
                shop.getShopLatitude(),
                shop.getShopStatus(),
                new Date(),
                shop.getShopUserId(),
                shop.getShopAccount(),
                shop.getShopPassword(),
                shop.getShopSort(),
                shop.getSalesVolume(),
                shop.getDistance(),
                shop.getShopCommunitySort(),
                shop.getConcernStatus(),
                shop.getShopOrderCount(),
                shop.getShopCategoryList(),
                shop.getShopGoodsList(),
                shop.getCommunityId()
        };
        int num = JDBCDruidUtil.update(sql, obj);
        return num;
    }

    @Override
    public int delete(int shopId) {
        return 0;
    }

    @Override
    public int update(Shop shop) {
        String sql = "update shop set shopName=?,shopPhone=?,shopProvince=?,shopCity=?,shopDistrict=?,shopAddress=?,shopLatitude=?,shopLongitude=?,shopDescribe=? where shopId=?";
        Object[] obj = new Object[]{
                shop.getShopName(),
                shop.getShopPhone(),
                shop.getShopProvince(),
                shop.getShopCity(),
                shop.getShopDistrict(),
                shop.getShopAddress(),
                shop.getShopLongitude(),
                shop.getShopLatitude(),
                shop.getShopDescribe(),
                shop.getShopId()
        };
        int num = JDBCDruidUtil.update(sql, obj);
        return num;
    }

    @Override
    public List<Shop> selectAll(Map<String, String> map) {
        List<Shop> shops = new ArrayList<>();
        List<Object> list = new ArrayList<>();

        String sql = "select * from shop where 1=1 ";
        String shopName = map.get("keyword");
        String startTime = map.get("startTime");
        String phone = map.get("shopPhone");
        String pageNum = map.get("pageNum");
        if (shopName != null && !"".equals(shopName)) {
            sql += " and shopName like ? ";
            list.add("%" + shopName + "%");
        }


        if (phone != null && !"".equals(phone)) {
            sql += " and shopPhone like ? ";
            list.add("%" + phone + "%");
        }

        if (startTime != null && !"".equals(startTime)) {
            sql += " and addTime between ? and ?";
            list.add(startTime);
            list.add(map.get("endTime"));
        }

        if (pageNum != null && !"".equals(pageNum)) {
            sql += " limit ?,? ";
            list.add((Integer.parseInt(pageNum) - 1) * Integer.parseInt(pageNum));
            list.add(Integer.parseInt(map.get("pageSize")));
        }

        Object[] obj = list.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true) {
            try {
                if (!res.next()) break;
                shops.add(new Shop(
                        res.getInt(1),
                        res.getString(2),
                        res.getString(3),
                        res.getString(4),
                        res.getString(5),
                        res.getString(6),
                        res.getString(7),
                        res.getString(8),
                        res.getString(9),
                        res.getString(10),
                        res.getString(11),
                        res.getInt(12),
                        res.getDate(13),
                        res.getInt(14),
                        res.getString(15),
                        res.getString(16),
                        res.getString(17),
                        res.getInt(18),
                        res.getString(19),
                        res.getString(20),
                        res.getInt(21),
                        res.getInt(22),
                        res.getString(23),
                        res.getString(24),
                        res.getInt(25)
                ));

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
        JDBCDruidUtil.close();
        return shops;
    }

    @Override
    public int selectCount(Map<String, String> map) {
        List<Shop> shops = new ArrayList<>();
        List<Object> list = new ArrayList<>();

        String sql = "select count(1) from shop where 1=1 ";
        String shopName = map.get("keyword");
        String startTime = map.get("startTime");
        String phone = map.get("shopPhone");


        if (shopName != null && !"".equals(shopName)) {
            sql += " and shopName like ?";
            list.add("%" + shopName + "%");
        }

        if (phone != null && !"".equals(phone)) {
            sql += " and shopPhone like ?";
            list.add("%" + phone + "%");
        }

        if (startTime != null && !"".equals(startTime)) {
            sql += " and addTime between ? and ?";
            list.add(startTime);
            list.add(map.get("endTime"));
        }


        Object[] obj = list.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        int total = 0;
        while (true) {
            try {
                if (!res.next()) break;
                total = res.getInt(1);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
        JDBCDruidUtil.close();
        return total;
    }
}
